<template>
  <div>
    <div :style="{ borderBottom: '1px solid #E9E9E9' }">
      <CloudCheckbox
        :indeterminate="indeterminate"
        :checked="checkAll"
        @change="onCheckAllChange"
      >
        Check all
      </CloudCheckbox>
    </div>
    <br />
    <CloudCheckboxGroup
      v-model="checkedList"
      :options="plainOptions"
      @change="onChange"
    />
  </div>
</template>

<script>
const plainOptions = ['Apple', 'Pear', 'Orange'];
const defaultCheckedList = ['Apple', 'Orange'];
export default {
  title: '4.全选 ',
  subTitle: '在实现全选效果时，你可能会用到indeterminate属性',
  data() {
    return {
      checkedList: defaultCheckedList,
      indeterminate: true,
      checkAll: false,
      plainOptions,
    }
  },
  methods: {
    onChange(checkedList) {
      this.indeterminate = !!checkedList.length && checkedList.length < plainOptions.length;
      this.checkAll = checkedList.length === plainOptions.length;
    },
    onCheckAllChange(e) {
      Object.assign(this, {
        checkedList: e.target.checked ? plainOptions : [],
        indeterminate: false,
        checkAll: e.target.checked,
      });
    },
  }
}
</script>

<style lang="scss">
</style>